/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.thienloc.omap.session;

import com.thienloc.omap.entity.BroadcastProduction;
import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.TypedQuery;
import org.slf4j.LoggerFactory;

/**
 *
 * @author GiangLT
 */
@Stateless
public class BroadcastProductionFacade extends AbstractFacade<BroadcastProduction> {

    @PersistenceContext(unitName = "omapPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public BroadcastProductionFacade() {
        super(BroadcastProduction.class);
    }

    public void deleteAll() {
        try {
            Query q = em.createNativeQuery("TRUNCATE TABLE broadcast_production");
            q.executeUpdate();
        } catch (Exception ex) {
            LoggerFactory.getLogger(BroadcastProductionFacade.class).error("", ex);
        }
    }

    public void upsertProduction(String username, String syntax, String provider, Integer production, Date startTime, Date endTime) {
        try {
            StringBuilder sql = new StringBuilder();
            sql.append(" MERGE INTO broadcast_production b USING ");

            sql.append(" (SELECT 1 FROM dual) e ");
            sql.append(" ON (user_name = ?1 ");
            sql.append(" AND syntax = ?2 ");
            sql.append(" AND provider = ?3");
            sql.append(" ) ");
            sql.append(" WHEN MATCHED THEN ");
            sql.append(" UPDATE SET ");
            sql.append(" b.production = ?4, ");
            sql.append(" b.start_time = ?5,  ");
            sql.append(" b.end_time = ?6, ");
            sql.append(" b.updated_time = sysdate");

            sql.append(" WHEN NOT MATCHED THEN ");
            sql.append(" INSERT (b.user_name, b.syntax, b.provider, b.production, b.start_time, b.end_time, b.updated_time) ");
            sql.append(" VALUES(?7, ?8, ?9, ?10, ?11, ?12, sysdate) ");
            Query q = em.createNativeQuery(sql.toString());
            q.setParameter(1, username);
            q.setParameter(2, syntax);
            q.setParameter(3, provider);
            q.setParameter(4, production);
            q.setParameter(5, startTime);
            q.setParameter(6, endTime);
            q.setParameter(7, username);
            q.setParameter(8, syntax);
            q.setParameter(9, provider);
            q.setParameter(10, production);
            q.setParameter(11, startTime);
            q.setParameter(12, endTime);
            q.executeUpdate();
        } catch (Exception ex) {
            LoggerFactory.getLogger(BroadcastProductionFacade.class).error("", ex);
        }
    }

    public void upsertBroadcastAmount(String username, String syntax, String provider, Integer broadcastAmount, Date startTime, Date endTime) {
        try {
            StringBuilder sql = new StringBuilder();
            sql.append(" MERGE INTO broadcast_production b USING ");

            sql.append(" (SELECT 1 FROM dual) e ");
            sql.append(" ON (user_name = ?1 ");
            sql.append(" AND syntax = ?2 ");
            sql.append(" AND provider = ?3");
            sql.append(" ) ");

            sql.append(" WHEN MATCHED THEN ");
            sql.append(" UPDATE SET ");
            sql.append(" b.broadcast = ?4, ");
            sql.append(" b.start_time = ?5,  ");
            sql.append(" b.end_time = ?6, ");
            sql.append(" b.updated_time = sysdate");

            sql.append(" WHEN NOT MATCHED THEN ");
            sql.append(" INSERT (b.user_name, b.syntax, b.provider, b.broadcast, b.start_time, b.end_time, b.updated_time) ");
            sql.append(" VALUES(?7, ?8, ?9, ?10, ?11, ?12, sysdate) ");
            Query q = em.createNativeQuery(sql.toString());
            q.setParameter(1, username);
            q.setParameter(2, syntax);
            q.setParameter(3, provider);
            q.setParameter(4, broadcastAmount);
            q.setParameter(5, startTime);
            q.setParameter(6, endTime);
            q.setParameter(7, username);
            q.setParameter(8, syntax);
            q.setParameter(9, provider);
            q.setParameter(10, broadcastAmount);
            q.setParameter(11, startTime);
            q.setParameter(12, endTime);
            q.executeUpdate();
        } catch (Exception ex) {
            LoggerFactory.getLogger(BroadcastProductionFacade.class).error("", ex);
        }
    }
    
    public List<BroadcastProduction> findKnownByProvider(String provider) {
        try {
            TypedQuery<BroadcastProduction> q = em.createNamedQuery("BroadcastProduction.findByProvider", BroadcastProduction.class);
            q.setParameter("provider", provider);
            List<BroadcastProduction> result = q.getResultList();
            return result;
        } catch (NoResultException ex) {
            return null;
        } catch (Exception ex) {
            LoggerFactory.getLogger(KqxsFacade.class.getName()).error("", ex);
            return null;
        }
    }
    
    public List<BroadcastProduction> findByTime(Date startTime, Date endTime) {
        try {
            TypedQuery<BroadcastProduction> q = em.createNamedQuery("BroadcastProduction.findByTime", BroadcastProduction.class);
            q.setParameter("startTime", startTime);
            q.setParameter("endTime", endTime);
            List<BroadcastProduction> result = q.getResultList();
            return result;
        } catch (NoResultException ex) {
            return null;
        } catch (Exception ex) {
            LoggerFactory.getLogger(KqxsFacade.class.getName()).error("", ex);
            return null;
        }
    }
}

